import userApiClient from "../../axios/user";
import type {
  UserListQueryParams,
  UserListResponse,
  CreateUserRequest,
  CreateUserResponse,
  GetUserResponse,
  UpdateUserRequest,
  UpdateUserResponse,
  DeleteUserResponse,
  UpdateUserStatusResponse
} from "../../dto/admin";

// 管理员相关API
export const adminApi = {
  // 获取用户列表
  getUserList: (params: UserListQueryParams): Promise<UserListResponse> => {
    const queryParams = new URLSearchParams();

    if (params.name) queryParams.append("name", params.name);
    if (params.systemRole)
      queryParams.append("systemRole", params.systemRole.toString());
    if (params.status) queryParams.append("status", params.status.toString());
    if (params.page) queryParams.append("page", params.page.toString());
    if (params.pageSize)
      queryParams.append("pageSize", params.pageSize.toString());
    if (params.sortBy) queryParams.append("sortBy", params.sortBy);
    if (params.sortOrder) queryParams.append("sortOrder", params.sortOrder);

    const queryString = queryParams.toString();
    const url = queryString ? `/admin/users?${queryString}` : "/admin/users";

    return userApiClient.get(url, {
      timeout: 30000
    });
  },

  // 创建用户
  createUser: (data: CreateUserRequest): Promise<CreateUserResponse> => {
    return userApiClient.post("/admin/users", data);
  },

  // 获取单个用户信息
  getUserById: (id: number): Promise<GetUserResponse> => {
    return userApiClient.get(`/admin/users/${id}`);
  },

  // 更新用户信息
  updateUser: (data: UpdateUserRequest): Promise<UpdateUserResponse> => {
    return userApiClient.post("/admin/update", data);
  },

  // 更新删除用户
  deleteUser: (id: string): Promise<DeleteUserResponse> => {
    return userApiClient.delete(`/admin/delete/${id}`);
  },

  updateUserStatus: (params: { id: string; status: number }): Promise<UpdateUserStatusResponse> => {
    return userApiClient.put(`/admin/status/${params.id}`, null, {
      params: {
        status: params.status
      }
    });
  }
};
